﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;


namespace ClickToCall.ClickToCallToolbar
{
    [RunInstaller(true)]
    public partial class LibInstaller : Installer
    {

        public LibInstaller()
        {
            InitializeComponent();
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Commit(System.Collections.IDictionary savedState)
        {
            base.Commit(savedState);

            string regasmPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() + @"regasm.exe";
            string componentPath = typeof(LibInstaller).Assembly.Location;
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.Arguments = " /codebase \"" + componentPath + "\"";
            p.StartInfo.FileName = regasmPath;
            p.StartInfo.CreateNoWindow = false;
            p.StartInfo.RedirectStandardOutput = true;

            try
            {
                p.Start();
            }
            catch(Exception) 
            {

            }
            finally 
            {
                p.WaitForExit();
                p.Close();
            }
        }

        protected override void OnAfterInstall(IDictionary savedState)
        {
            base.OnAfterInstall(savedState);

            string dir = "", file = "";
            try
            {
                dir = System.IO.Path.GetDirectoryName(typeof(LibInstaller).Assembly.Location);
                file = dir + "\\configuration.exe";
                if (!System.IO.File.Exists(file))
                    return;

                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.Arguments = file;
                p.Start();
                return;
            }
            catch (Exception)
            {
                return;
            }
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            base.Install(stateSaver);
            
        }

        [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);

            string regasmPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory() + @"regasm.exe";
            string componentPath = typeof(LibInstaller).Assembly.Location;
            System.Diagnostics.Process.Start(regasmPath, " /unregister \"" + componentPath + "\"");
        }

    }
}
